/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.comut.crud.bo;

import com.comut.crud.dao.TrailerDao;
import com.comut.crud.ejb.interfaces.ITrailerAction;
import com.comut.crud.pojo.FindAndSet;
import com.comut.crud.pojo.mobile.TrailerDetail;
import com.comut.crud.pojo.mobile.TrailerInfo;
import com.comut.crud.pojo.trailer.Trailer;
import com.comut.crud.util.ParseJson;
import com.comut.lib.connection.MongoDBConnectionManager;
import com.comut.lib.error.ErrorCodes;
import com.comut.lib.exception.JSONMalFormedException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.apache.log4j.Logger;

/**
 *
 * @author asantos
 */
public class TrailerHandler implements ITrailerAction {

    private static Logger _logger = Logger.getLogger(TrailerHandler.class.getName());
    private Connection _connection;
    private TrailerDao trailerDao;
    private MongoDBConnectionManager _mongodb;

    public TrailerHandler(Connection connection) {
        this._connection = connection;
        trailerDao = new TrailerDao(_connection);
    }

    /**
     * Issue: IFL-1572
     *
     * @param connection
     * @param mongodb
     */
    public TrailerHandler(Connection connection, MongoDBConnectionManager mongodb) {
        this._connection = connection;
        this._mongodb = mongodb;
        trailerDao = new TrailerDao(_connection, _mongodb);
    }

    public Trailer insertTrailer(Trailer trailer) {
        if (trailer.isValid()) {
            int id = trailerDao.createTrailer(trailer);
            Trailer toRet = new Trailer();
            toRet.setId(id);
            _logger.info(toRet.toString());
            return toRet;
        } else {
            throw new JSONMalFormedException("The mandatory attributes are not in json.", ErrorCodes.JSON_MAL_FORMED);
        }
    }

    public void deleteTrailer(Trailer trailer) {
        _logger.info(trailer.toString());
        trailerDao.deleteTrailer(trailer.getId());
    }

    public void updateTrailer(String trailer) {
        _logger.info(trailer.toString());

        //check if the BasicDBObject is well formed
        FindAndSet fas = ParseJson.getInstance().buildSQLQuery(trailer, ParseJson.getInstance().getTrailerFind(), ParseJson.getInstance().getTrailerUpdate(),
                ParseJson.getInstance().getTrailerFindStr(), ParseJson.getInstance().getTrailerUpdateStr());

        //update the trailer
        trailerDao.updateTrailer(fas);
    }

    /**
     *  @author celia calcada
     *  ...
     */
    public Trailer getTrailer(Trailer trailer) {
        ResultSet rs = trailerDao.getTrailer(trailer.getId());
        Trailer toRet = new Trailer();
        try {
            if (rs.next()) {
                toRet.setId(trailer.getId());
                toRet.setThermo_id(rs.getLong("thermo_id"));
            }
        } catch (SQLException ex) {
            _logger.error(ex);
        }

        _logger.info(toRet.toString());
        return toRet;
    }

    /**
     * Issue: IFL-1572
     * @author celia calcada
     * @param userId
     * @return
     */
    public TrailerInfo[] getTrailersForUserId(int userId) {
        return trailerDao.getTrailersForUserId(userId);
    }

    /**
     * Issue: IFL-1573
     * @author celia calcada
     * @param userId
     * @param trailerId
     * @return
     */
    public TrailerDetail getTrailerDetail(int userId, int trailerId) {
        ResultSet resultSet = trailerDao.getTrailerDetail(userId, trailerId);
        TrailerDetail trailerDetail = null;

        try {
            if (resultSet.next()) {
                trailerDetail = new TrailerDetail();
                trailerDetail.setInspectionDate(resultSet.getDate("inspectionDate"));
                trailerDetail.setThermographInspectionDate(resultSet.getDate("thermographInspectionDate"));
                trailerDetail.setThermographCertification(resultSet.getString("thermographCertification"));
            }
        } catch (Exception ex) {
            _logger.error(ex);
        }
        
        return trailerDetail;
    }
}
